﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Data;

using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;
using DevExpress.Data;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CARSpoc : ICPRelationshipPage
{
    
    #region Properties
    public String m_sxFilter = String.Empty;
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Monitoring SPOC";
        if (!IsPostBack && !IsCallback)
        {
            Session[KSession.CARSPOC_FILTER] = null;
            Session[KSession.CARSPOC_SORTBY] = null;
        }
        //DoSort();
        InitSpoc();
        //DoSort();
    }
    protected void dgSpoc_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if (e.Parameters.ToUpper().StartsWith("FILTER"))
        {
            HandleFilter(e.Parameters);
        }
        else if (e.Parameters.ToUpper().StartsWith("ALL"))
        {
            Session[KSession.CARSPOC_FILTER] = null;
            InitSpoc();
            dgSpoc.CollapseAll();
        }
        else if (e.Parameters.ToUpper().StartsWith("SORT"))
        {
            String[] sxSplitted = e.Parameters.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
            Session[KSession.CARSPOC_SORTBY] = sxSplitted[1];
            //DoSort(sxSortBy);
            InitSpoc();
        }
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected void InitSpoc()
    {
        if (Session[KSession.CARSPOC_FILTER] != null)
        {
            //disini ambil query yang pake filter
            Cached.CARSpocTable cachedCARSpoc = new Cached.CARSpocTable();
            m_sxFilter = (String)Session[KSession.CARSPOC_FILTER];
            DataTable dtCARSpoc = cachedCARSpoc.GetCARSpocListByCar(m_sxFilter);

            dgSpoc.DataSource = dtCARSpoc;
            dgSpoc.DataBind();
            //DoSort();
        }
        else
        {
            //disini ambil query all
            Cached.CARSpocTable cachedCARSpoc = new Cached.CARSpocTable();
            DataTable dtCARSpoc = cachedCARSpoc.GetCARSpocList();

            dgSpoc.DataSource = dtCARSpoc;
            dgSpoc.DataBind();
            //DoSort();
        }
        //DoSort();
        if (Session[KSession.CARSPOC_SORTBY] != null)
        {
            String sxSortBy = Session[KSession.CARSPOC_SORTBY].ToString();
            DoSort(sxSortBy);
        }
    }
    protected void HandleFilter(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        Session[KSession.CARSPOC_FILTER] = sxSplitted[1];
        InitSpoc();
        //DoSort();
    }
    //protected void HandleSort(String sxParameter)
    //{
    //    String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
    //    Session[KSession.CARSPOC_SORT] = sxSplitted[1];
    //    InitSpoc();
    //    //DoSort();
    //}
    //protected void DoSortzzzz()
    //{
    //    String sxSort = Session[KSession.CARSPOC_SORT] != null ?
    //        (String)Session[KSession.CARSPOC_SORT] :
    //        "ASCENDING";

    //    ColumnSortOrder sortOrder = sxSort.Equals("ASCENDING") ? 
    //        ColumnSortOrder.Ascending : 
    //        ColumnSortOrder.Descending;

    //    ASPxGroupSummarySortInfo sortInfo = 
    //        new ASPxGroupSummarySortInfo("EMPLOYEE_NAME", dgSpoc.GroupSummary["TOTAL_REV"], sortOrder);

    //    dgSpoc.GroupSummarySortInfo.AddRange(sortInfo);
    //}
    protected void DoSort(String sxSortBy)
    {
        dgSpoc.GroupSummarySortInfo.Clear();
        ColumnSortOrder sortOrder = sxSortBy.ToUpper().Equals("DESCENDING") ? ColumnSortOrder.Descending : ColumnSortOrder.Ascending;
        dgSpoc.GroupSummarySortInfo.ClearAndAddRange(new ASPxGroupSummarySortInfo("CAR_NAME",
            dgSpoc.GroupSummary["TOTAL_REV"], sortOrder));
    }
    #endregion Custom Methods
}
